

var cursorPosition = {
    get: function (textarea) {
        var rangeData = {text: "", start: 0, end: 0 };
     
        if (textarea.setSelectionRange) { // W3C   
            textarea.focus();
            rangeData.start= textarea.selectionStart;
            rangeData.end = textarea.selectionEnd;
            rangeData.text = (rangeData.start != rangeData.end) ? textarea.value.substring(rangeData.start, rangeData.end): "";
        } else if (document.selection) { // IE
            textarea.focus();
            var i,
                oS = document.selection.createRange(),
                oR = document.body.createTextRange();
            oR.moveToElementText(textarea);
             
            rangeData.text = oS.text;
            rangeData.bookmark = oS.getBookmark();
             
            for (i = 0; oR.compareEndPoints('StartToStart', oS) < 0 && oS.moveStart("character", -1) !== 0; i ++) {
 
                if (textarea.value.charAt(i) == '\r' ) {
                    i ++;
                }
            }
            rangeData.start = i;
            rangeData.end = rangeData.text.length + rangeData.start;
        }
         
        return rangeData;
    },
     
    set: function (textarea, rangeData) {
        var oR, start, end;
        if(!rangeData) {
            alert("You must get cursor position first.")
        }
        textarea.focus();
        if (textarea.setSelectionRange) { // W3C
            textarea.setSelectionRange(rangeData.start, rangeData.end);
        } else if (textarea.createTextRange) { // IE
            oR = textarea.createTextRange();
            if(textarea.value.length === rangeData.start) {
                oR.collapse(false);
                oR.select();
            } else {
                oR.moveToBookmark(rangeData.bookmark);
                oR.select();
            }
        }
    },
 
    add: function (textarea, rangeData, text) {
        var oValue, nValue, oR, sR, nStart, nEnd, st;
        this.set(textarea, rangeData);
         
        if (textarea.setSelectionRange) { // W3C
            oValue = textarea.value;
            nValue = oValue.substring(0, rangeData.start) + text + oValue.substring(rangeData.end);
            nStart = nEnd = rangeData.start + text.length;
            st = textarea.scrollTop;
            textarea.value = nValue;
            if(textarea.scrollTop != st) {
                textarea.scrollTop = st;
            }
            textarea.setSelectionRange(nStart, nEnd);
        } else if (textarea.createTextRange) { // IE
            sR = document.selection.createRange();
            sR.text = text;
            sR.setEndPoint('StartToEnd', sR);
            sR.select();
        }
    }
}
 
 
var tx=document.getElementById("g_body"),
    pos;


		//选择图片
		$("#list_li li a").live('click',function(){
			var pid = $(this).attr('data_pid');
			var img = $(this).find('img').attr('src');
			alert(pid);					
			if (pid=='demo_image') {								
				$('#demo_image').attr('src',img);
			} else if(pid=='g_body'){
				pos = cursorPosition.get(tx);
				var img_html = '<img src="'+img+'" data-ke-src="'+img+'">' ;
				var son = $('#g_body').val();
				
				cursorPosition.add(tx, pos, img_html);
				//$('#g_body').children().after(img_html);
			}
								
		}); 
	
/*document.getElementById("get").onclick = function(){
    pos = cursorPosition.get(tx);
    re.innerHTML=("<strong>Range :</strong> (" + pos.start + ", " + pos.end + ")<br /><strong>Text :</strong> " + (!pos.text ? '//--': pos.text));
}
 
document.getElementById("set").onclick = function(){
    cursorPosition.set(tx, pos);
}*/

/* document.getElementById("show_desc").onclick = function(){
	pos = cursorPosition.get(tx);
    cursorPosition.add(tx, pos, input = prompt("你想插入替换的文本：",""));
} */
